<?php
/*
 *公用函数库
 */

/**
 * utf8转成gbk
 * @param str str 字符串
 * @return str
 */
function utf8_to_gbk($str)
{
	return iconv("UTF8","GBK",$str);
}
/**
 * gbk转成utf8
 * @param str str 字符串
 * @return str
 */
function gbk_to_utf8($str)
{
	return iconv("GBK","UTF8",$str);
}

/**
 * url导向
 * @param str url	地址
 * @return void
 */
function url_redirect($url)
{
	if($url != "") {
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>';
		echo '<script language="javaScript">';
		echo 'window.location = "'.$url.'";';
		echo '</script>';
		echo '</body></html>';
	}
}
/**
 * 返回信息导向
 *
 * @param str msg	信息
 * @param str url	地址
 * @return void
**/
function url_msg_redirect($msg,$url)
{
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>';
	echo '<script language="javaScript">';
	echo 'alert("'.$msg.'");';
	echo 'window.location = "'.$url.'";';
	echo '</script>';
	echo '</body></html>';
}
/**
 * 获取客户端ip
 * @return string
**/
function get_client_ip()
{
	$user_ip = "";
	if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
		$user_ip = getenv('HTTP_CLIENT_IP');
	} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
		$user_ip = getenv('HTTP_X_FORWARDED_FOR');
	} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
		$user_ip = getenv('REMOTE_ADDR');
	} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
		$user_ip = $_SERVER['REMOTE_ADDR'];
	}
	return $user_ip;
}

function rcolor()
{
	$rand = rand(0,255);
	return sprintf("%02X","$rand");
}
/**
 * 随机一个颜色
 */
function rand_color()
{
	return '#'.rcolor().rcolor().rcolor();
}

/**
 * 返回协议包(可以再定)
 * @param str data  pack后的包体
 * @param int cmd1	一级命令符
 * @param int cmd2	二级命令符
 */
function output($data,$cmd1,$cmd2){
	$len = strlen($data) + strlen(pack("CC",0,0));
	$output = pack("NCC",$len, $cmd1, $cmd2).$data;
	return $output;
}

?>
